import Mock from 'mockjs';
import qs from 'qs';
import { BaseUrl } from '@/api/const';

const base = '/dict';

const len = 6;
// const incomeCategory = /工资|其他/;
// const outlayCategory = /吃饭|公交|地铁|购物|网购/;
const regCategory = {
  income: /工资|其他/,
  outlay: /购物|吃饭|交通/
};

function productData(type) {
  return Mock.mock({
    code: 0,
    message: '获取分类成功',
    data: function () {
      let result = [];
      for (let i = 0; i < len; i++) {
        result.push(productDataItem(regCategory[type]));
      }
      return result;
    }
  });
}
function productDataItem(category) {
  return Mock.mock({
    id: /[a-zA-Z0-9]{10}/,
    category: category
  });
}

// 拦截带参数请求，需用正则匹配路径
function regUrl(url: string) {
  return RegExp(BaseUrl() + url + '.*');
}

Mock.mock(regUrl(`${base}/list`), 'get', (options) => {
  const params = qs.parse(options.url.split('?')[1]);
  // console.log(params);
  return productData(params.type);
});
